//
//  SousuoVC.m
//  Hand-held_kitchen
//
//  Created by 张张烨 on 16/1/22.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SousuoVC.h"
#import "ZongTouwenjian.pch"
#import "ShouView.h"
#import "SousuoCell.h"
#import "sousuoMoble.h"
#import "SSErjiVc.h"
#import "SSquedingVC.h"

@interface SousuoVC ()<UITableViewDataSource,UITableViewDelegate,UISearchResultsUpdating,UISearchBarDelegate>
@property(nonatomic,retain)ShouView *view1;
@property(nonatomic,retain)UITableView *tableView;
@property (nonatomic, retain) UISearchController *searchController;
@property(nonatomic,retain)NSMutableArray *dataArr;
@property(nonatomic,copy)NSString *string;
@property(nonatomic,retain)sousuoMoble *moble;

@end

@implementation SousuoVC
-(void)dealloc
{
    [_view1 release];
    [_tableView release];
    [_searchController release];
    [_dataArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

//    self.view1 = [[ShouView alloc]initWithFrame:CGRectMake(0, 200, Width, 467)];
//    _tableView.tableHeaderView = _view1;
//    _view1.backgroundColor = [UIColor yellowColor];
    
//    UIView *gaoView = [[UIView alloc]initWithFrame:CGRectMake(0, 22, Width, 200)];
//    gaoView.backgroundColor = [UIColor grayColor];
    self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    //    设置开始搜索时背景显示与否 页面背景阴影效果,一般不要设置.
    self.searchController.dimsBackgroundDuringPresentation = NO;
    //    //设置点击搜索框时候隐藏导航栏
        self.searchController.hidesNavigationBarDuringPresentation = NO;
//        self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
        [self.searchController.searchBar sizeToFit];  //位置自适应

    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"搜索"] style:(UIBarButtonItemStylePlain) target:self action:@selector(youdaohang:)];
    self.navigationItem.rightBarButtonItem  =rightItem;
    
   
    
    
    [self table1];
   [self.view addSubview:_view1];
    self.navigationItem.titleView = _searchController.searchBar;
    
//    [gaoView release];
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
-(void)youdaohang:(UIBarButtonItem *)bu
{
    if (self.string.length == 0 || [self.string containsString:@" "]) {
        
    }else{
    SSquedingVC *dierye = [[SSquedingVC alloc]init];
    dierye.str = self.string;
    [self.navigationController pushViewController:dierye animated:YES];
    }
}
-(void)table1
{
    self.tableView = [[UITableView alloc]initWithFrame:Frame style:(UITableViewStylePlain)];
    _tableView.dataSource =self;
    _tableView.delegate =self;
    [_tableView registerClass:[SousuoCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    [_tableView release];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SousuoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    sousuoMoble *m = _dataArr[indexPath.row];

//    cell.textLabel.text =m.text;

    
    
    ///高亮设置
    NSMutableAttributedString *attrituteString = [[NSMutableAttributedString alloc] initWithString:m.text];
    NSRange range = [m.text rangeOfString:_string];
    [attrituteString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor],   NSFontAttributeName : [UIFont systemFontOfSize:20]} range:range];
    cell.textLabel.attributedText = attrituteString;
    
    
    
    
    
    
    
    

    
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SSErjiVc *er = [[SSErjiVc alloc]init];
    sousuoMoble *m = _dataArr[indexPath.row];
    er.str = m.id1;
 
    [self.navigationController pushViewController:er animated:YES];
}






// 导航搜索框协议方法
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    
    
    
    
    
    
    
    
    
    
    
  
    NSString *searchString = [self.searchController.searchBar text];
    self.string =[self.searchController.searchBar text];
    
    NSLog(@"..`````````®");
   
   
    
    
    
    
    
    
    if ([searchString containsString:@" "]) {

    }else if(searchString.length == 0){
        [self.view bringSubviewToFront:_view1];
    }else
    {
    /**<  把view拿到前面 */
    [self.view bringSubviewToFront:_tableView];
    
    
    NSString* str = [self.searchController.searchBar text];
        
     NSString *urlStr = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:str]];
    NSString *str1 = [NSString stringWithFormat:@"http://api.izhangchu.com/?methodName=SearchKeyword&keyword=%@&version=4.3&", urlStr];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:str1 parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        sousuoMoble *m = [[sousuoMoble alloc]init];
//        if (searchString.length == 0) {
//            NSLog(@"kkk");
//        }else{
//            m.searchText = searchString;
//            NSLog(@",,,%@",m.searchText);
//        }
        
        
//        m.searchText =[self.searchController.searchBar text];
        
        NSArray *arr = responseObject[@"data"][@"data"];
        self.dataArr = [sousuoMoble modelHanderWithArray:arr];
//        [_dataArr addObject:m.searchText];
        [_tableView reloadData];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
    
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
